package com.arunsawad.ilertu.activity;

import java.util.Locale;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.TextView;

import com.arunsawad.ilertu.R;
import com.arunsawad.ilertu.common.Constants;
import com.arunsawad.ilertu.common.ILertU;

public class Index extends Activity {
	SharedPreferences preferences;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.act_index);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
				R.layout.title0_imgbtn_txt);
		
		initTitle();
		
		
		//android.util.DisplayMetrics metrics = getResources().getDisplayMetrics();

		preferences = getSharedPreferences(Constants.PREF_FILE, MODE_PRIVATE);

		// force localization
		String language = preferences.getString(Constants.PREF_LANGUAGE, "en");
		Locale locale = new Locale(language);
		Locale.setDefault(locale);
		Configuration config = new Configuration();
		config.locale = locale;
		getBaseContext().getResources().updateConfiguration(config,
				getBaseContext().getResources().getDisplayMetrics());

		// validation
		String username = preferences.getString(Constants.PREF_USER_NAME, "");
		if (!username.equals("")) {
			Intent intent = new Intent(this, Home.class);
			startActivity(intent);
		}
	}
	
	public void initTitle() {
		ImageButton imgBtn1 = (ImageButton) findViewById(R.id.title0_imgBtn1);
		imgBtn1.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(getApplicationContext(), Home.class);
				startActivity(intent);
			}
		});

		TextView txtTitle = (TextView) findViewById(R.id.title0_txtTitle);
		txtTitle.setText(getString(R.string.app_name));
	}

	// {{ onClick events
	public void signIn(View view) {
		Intent intent = new Intent(this, SignIn.class);
		startActivity(intent);
	}

	public void signUp(View view) {
		((ILertU) this.getApplication()).setImagePath("");

		Intent intent = new Intent(this, SignUp.class);
		startActivity(intent);
	}

	// }}
}
